﻿using ASmile.DBModel;
using ASmile.Extend;
using ASmile.ORM.Cores;

namespace ASmile.Com.Da
{
    public class CfgGlobalBLL : BLLCrudBase<Cfg_Global>, IBLLCrud<Cfg_Global>
    {
        protected override void SetQuery(QueryCondition<Cfg_Global> query, ArgPageData arg)
        {
            if (!arg.SearchVal.IsEmpty())
            {
                string likeStr = $"%{arg.SearchVal}%";
                query.WhereAnd(s =>
                    s.WEx_Like(s.ClassCaption, likeStr) ||
                    s.WEx_Like(s.PropertyCaption, likeStr)
                    );
            }
        }

        protected override ApiResult<string> VerifyModel(Cfg_Global model, bool isAdd)
        {
            if (model.ClassCaption.IsEmpty())
            {
                return RetErr(nameof(model.ClassCaption), "类说明不能为空");
            }
            if (model.PropertyName.IsEmpty())
            {
                return RetErr(nameof(model.PropertyName), "属性名不能为空");
            }
            if (model.PropertyCaption.IsEmpty())
            {
                return RetErr(nameof(model.PropertyCaption), "属性说明不能为空");
            }
            if (isAdd)
            {
                model.Id = NewId();
                return RetOK(model.Id, "新增成功");
            }
            return RetOK(string.Empty, "保存成功");
        }
    }
}